package com.cvo.scrumtoolkit.client.pannels.widgets;

import com.cvo.scrumtoolkit.client.controller.ControllerImpl;
import com.cvo.scrumtoolkit.client.controller.HistoryManager;
import com.cvo.scrumtoolkit.client.controller.Navigator;
import com.cvo.scrumtoolkit.client.controller.View;
import com.cvo.scrumtoolkit.client.entities.Project;
import com.cvo.scrumtoolkit.client.entities.WorkItem;
import com.cvo.scrumtoolkit.client.model.ProjectStore;
import com.cvo.scrumtoolkit.client.pannels.PersistentService;
import com.cvo.scrumtoolkit.client.pannels.PersistentServiceAsync;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.user.client.ui.TextBox;

public class WorkitemInSprintWidget extends Composite {
	private final PersistentServiceAsync persistentService = GWT
			.create(PersistentService.class);
	HorizontalPanel hp = new HorizontalPanel();
	Button btnWorkitemtoevoegen = new Button();
	HorizontalPanel voorkantPanel = new HorizontalPanel();
	HorizontalPanel achterlPanel = new HorizontalPanel();
	Label lblNaam = new Label("");
	TextBox tbUur = new TextBox();
	Project project;

	public WorkitemInSprintWidget(final WorkItem w) {
		hp.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		initWidget(hp);
		hp.setSize("400px", "30px");
		ProjectStore projectStore = ProjectStore.getProjectStore();
		try {
			String id = Navigator.getNavigator().getProjectId();
			String sprint = Navigator.getNavigator().getSprintId();
			String string = "sprintbacklog/" + id + "/" + sprint;
			HistoryManager.gotoUrl(string);

		} catch (Exception ex) {
			// TODO: handle exception
		}
		checkStatus(w.getStatus());

		hp.add(voorkantPanel);
		hp.add(achterlPanel);
		achterlPanel.setVisible(false);
		voorkantPanel.setSize("400px", "40px");

		Label lblBacklogitemnaam = new Label("backlogItemNaam");
		voorkantPanel.add(lblBacklogitemnaam);
		voorkantPanel.setCellVerticalAlignment(lblBacklogitemnaam,
				HasVerticalAlignment.ALIGN_MIDDLE);
		lblBacklogitemnaam.setText(w.getWorkitemname());
		lblBacklogitemnaam
				.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		lblBacklogitemnaam.setStyleName("");
		hp.setCellVerticalAlignment(lblBacklogitemnaam,
				HasVerticalAlignment.ALIGN_MIDDLE);
		lblBacklogitemnaam.setSize("150px", "20px");

		voorkantPanel.add(lblNaam);
		voorkantPanel.setCellVerticalAlignment(lblNaam,
				HasVerticalAlignment.ALIGN_MIDDLE);
		voorkantPanel.setCellHorizontalAlignment(lblNaam,
				HasHorizontalAlignment.ALIGN_CENTER);
		hp.setCellHorizontalAlignment(lblNaam,
				HasHorizontalAlignment.ALIGN_CENTER);
		lblNaam.setSize("146px", "20px");

		voorkantPanel.add(btnWorkitemtoevoegen);
		voorkantPanel.setCellHorizontalAlignment(btnWorkitemtoevoegen,
				HasHorizontalAlignment.ALIGN_RIGHT);
		btnWorkitemtoevoegen.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {

				if (btnWorkitemtoevoegen.getText().equals("Take")) {
					w.setStatus("geel");
					persistentService.setStatus(w.getWorkitemname(), "geel",
							new AsyncCallback<Void>() {

								@Override
								public void onFailure(Throwable caught) {// TODO
																			// Auto-generated
																			// method
																			// stub
									Window.alert("NOK FAIL");
								}

								@Override
								public void onSuccess(Void result) {
									System.out
											.println("De status van het workitem is veranderd");
									try {
										String id = Navigator.getNavigator()
												.getProjectId();
										String sprint = Navigator
												.getNavigator().getSprintId();
										String string = "sprintbacklog/" + id
												+ "/" + sprint;
										HistoryManager.gotoUrl(string);

									} catch (Exception ex) {
										Window.alert("NOK");
									}
									/*
									 * try {
									 * ControllerImpl.getController().changeView
									 * (View.SPRINTBACKLOG,
									 * project.getId().toString(), null); }
									 * catch (Exception ex) { // TODO
									 * Auto-generated catch block }
									 */
								}
							});

				} else {
					achterlPanel.setVisible(true);

				}

			}
		});

		hp.setCellVerticalAlignment(btnWorkitemtoevoegen,
				HasVerticalAlignment.ALIGN_MIDDLE);
		hp.setCellHorizontalAlignment(btnWorkitemtoevoegen,
				HasHorizontalAlignment.ALIGN_RIGHT);
		btnWorkitemtoevoegen.setSize("50px", "39px");

		Label lblGewerkteUren = new Label("Gewerkte uren:");
		achterlPanel.add(lblGewerkteUren);
		achterlPanel.setCellVerticalAlignment(lblGewerkteUren,
				HasVerticalAlignment.ALIGN_MIDDLE);
		lblGewerkteUren.setSize("77px", "40px");

		achterlPanel.add(tbUur);
		tbUur.setSize("51px", "22px");

		Button btnOk = new Button("ok");
		btnOk.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				w.setGewerkteUren(Integer.parseInt(tbUur.getText()));
				w.setStatus("groen");
				persistentService.setWerkuren(w.getWorkitemname(),
						Integer.parseInt(tbUur.getText()),
						new AsyncCallback<Void>() {

							@Override
							public void onFailure(Throwable caught) {// TODO
																		// Auto-generated
																		// method
																		// stub
							}

							@Override
							public void onSuccess(Void result) {
								System.out
										.println("De status van het workitem is veranderd");
								wachteenbeetje();
							}
						});
				try {
					String id = Navigator.getNavigator().getProjectId();
					String sprint = Navigator.getNavigator().getSprintId();
					String string = "sprintbacklog/" + id + "/" + sprint;
					HistoryManager.gotoUrl(string);

				} catch (Exception ex) {
					// TODO: handle exception
				}
			}
		});
		achterlPanel.add(btnOk);
		btnOk.setHeight("33px");

	}

	public void checkStatus(String kleur) {
		if (kleur.equals("geel")) {
			voorkantPanel.setStyleName("worktitemGeel");
			btnWorkitemtoevoegen.setText("Done");
			lblNaam.setText("Yves Bosmans");
		} else if (kleur.equals("groen")) {
			voorkantPanel.setStyleName("workitemGroen");
			achterlPanel.setVisible(false);
			voorkantPanel.remove(btnWorkitemtoevoegen);
		} else {
			voorkantPanel.setStyleName("worktitemRood");
			btnWorkitemtoevoegen.setText("Take");
		}

	}

	public void wachteenbeetje() {
		Timer t = new Timer() {

			@Override
			public void run() {
				try {
					String id = Navigator.getNavigator().getProjectId();
					String sprint = Navigator.getNavigator().getSprintId();
					String string = "sprintbacklog/" + id + "/" + sprint;
					HistoryManager.gotoUrl(string);

				} catch (Exception ex) {
					// TODO: handle exception
				}

			}

		};
		t.schedule(7000);
	}
}
